﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

public partial class Course_Assessment : System.Web.UI.Page
{
    public string AssessmentLocation
    {
        get
        {
            if (Request.QueryString["ALoc"] != null)
                return Request.QueryString["ALoc"];
            else
                return "";
        }
    }

    public int CourseID
    {
        get
        {
            int ID = 0;
            if (Request.QueryString["Cid"] != null)
                Int32.TryParse(Request.QueryString["Cid"], out ID);
            return ID;
        }
    }

    public int AssessmentID
    {
        get
        {
            int ID = 0;
            if (Request.QueryString["Aid"] != null)
                Int32.TryParse(Request.QueryString["Aid"], out ID);
            return ID;
        }
    }

    public string AuthFail;
    public string RedirectLoc;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        AuthFail = "false";
        if (!Page.User.Identity.IsAuthenticated)
        {
            AuthFail = "true";
            RedirectLoc = ResolveClientUrl("~/default.aspx?Message=Authentication Failed - Please Login Again");
            return;
        }

        LMSData.AssessmentController ac = new LMSData.AssessmentController();
        LMSData.UserAssessmentController uac = new LMSData.UserAssessmentController();
        LMSData.AssessmentResponseController arc = new LMSData.AssessmentResponseController();
        LMSData.AssessmentItemController aic = new LMSData.AssessmentItemController();
        LMSData.AssessmentItemCollection aicl = aic.FetchByAid(AssessmentID);
        bool AssessmentTaken = uac.AssessmentTaken(CourseID, User.Identity.Name, AssessmentID);
        LMSData.AssessmentResponseCollection coll = arc.Fetch(CourseID, User.Identity.Name, AssessmentID);
        bool AssessmentItemsProcessed = ac.AssessmentItemsProcessed(AssessmentID);

        if (AssessmentTaken)
        {
            lblStatus.Text = "Assessment Taken - You can only review questions<br><br>";
            btnSubmit.Visible = false;
        }

        Assessment asmt = new Assessment(Server.MapPath(AssessmentLocation), AssessmentLocation);
        //Assessment asmt = new Assessment(@"C:\Code\IMSWeb\LMSWeb\Course\Courses\6\I_00004_R\Assessment.xml", "~/Course/Courses/exmpldocs/ims_qtiasiv1p2p1_examples/basic/ctpt_i_001/");

        int i = 0;
        foreach (AssessmentItem ai in asmt.AssessmentItems)
        {
            if (!AssessmentItemsProcessed)
            {
                LMSData.AssessmentItem aItem = new LMSData.AssessmentItem();
                aItem.AssessmentID = AssessmentID;
                aItem.ItemText = ai.ItemText;
                aItem.Save();
                ai.AssessmentItemID = aItem.AssessmentItemID;
            }

            //ASP.course_usercontrols_assessmentquestion_ascx aq = new ASP.course_usercontrols_assessmentquestion_ascx();
            Course_UserControls_AssessmentQuestion aq = (Course_UserControls_AssessmentQuestion)LoadControl("~/Course/UserControls/AssessmentQuestion.ascx");

            if (i < aicl.Count)
                aq.AssessmentItemID = aicl[i].AssessmentItemID;
            
            if (AssessmentTaken)
            {
                foreach (LMSData.AssessmentResponse ar in coll)
                {
                    if(ar.AssessmentItemID == aq.AssessmentItemID)
                        ai.AnswerGiven = ar.Response;
                }
            }
            aq.Data = ai;
            pnlQuestions.Controls.Add(aq);
            i++;
        }
    }

    protected void SubmitAnswers(object sender, EventArgs e)
    {
        int uaid = -1;

        LMSData.UserAssessmentController uac = new LMSData.UserAssessmentController();
        uaid = uac.CreateRecord(CourseID, User.Identity.Name, AssessmentID);
        if (uaid == -1)
        {
            lblStatus.Text = "Error: Assessment Taken - Results Not Submitted";
            return;
        }

        LMSData.AssessmentResponseController arc = new LMSData.AssessmentResponseController();
        foreach (Control ctrl in pnlQuestions.Controls)
        {
            ((Course_UserControls_AssessmentQuestion)ctrl).ShowSelectedAnswerFeedBack();
            AssessmentResponse rsp = ((Course_UserControls_AssessmentQuestion)ctrl).GetResponse();
            LMSData.AssessmentResponse ar = new LMSData.AssessmentResponse();
            ar.UserAssessmentID = uaid;
            ar.AssessmentItemID = ((Course_UserControls_AssessmentQuestion)ctrl).AssessmentItemID;
            ar.Response = rsp.RefId;
            ar.Score = rsp.Score;
            ar.Save();
        }
        lblStatus.Text = "Assessment Taken - You can only review questions<br><br>";
        btnSubmit.Visible = false;
    }

    private int GetUserID()
    {
        LMSData.UserController uc = new LMSData.UserController();
        return uc.GetUserID(User.Identity.Name);
    }
}
